package com.aboo.action;

import com.aboo.api.CompanyService;
import com.aboo.domain.Employee;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/consumer")
public class ConsumerCompanyAction {

    @Reference(group = "enterprise", version = "1.0")
    private CompanyService companyService01;
    @Reference(group = "enterprise", version = "1.1")
    private CompanyService companyService02;

    @RequestMapping("/company01")
    public Map<String, Object> company01() {
        Map<String, Object> result = new HashMap<>(8);
        result.put("flag", "Y");
        result.put("desc", "这是一个dubbo消费者1.0测试");

        Map<String, Object> data = new HashMap<>(16);
        String name = "yata";
        String tip = companyService01.callEmployee(name);
        BigDecimal money = companyService01.calcMoney(26);
        List<Employee> employees = companyService01.getEmployee(5);
        data.put("name", name);
        data.put("tip", tip);
        data.put("money", money);
        data.put("employees", employees);

        result.put("data", data);
        log.info(JSONObject.toJSONString(result));

        return result;
    }

    @RequestMapping("/company02")
    public Map<String, Object> company02() {
        Map<String, Object> result = new HashMap<>(8);
        result.put("flag", "Y");
        result.put("desc", "这是一个dubbo消费者1.1测试");

        Map<String, Object> data = new HashMap<>(16);
        String name = "yata";
        String tip = companyService02.callEmployee(name);
        BigDecimal money = companyService02.calcMoney(26);
        List<Employee> employees = companyService02.getEmployee(5);
        data.put("name", name);
        data.put("tip", tip);
        data.put("money", money);
        data.put("employees", employees);

        result.put("data", data);
        log.info(JSONObject.toJSONString(result));

        return result;
    }

}
